<?php
namespace Admin\Controller;
use Think\Controller;
class CommonController extends Controller {

    public function _initialize()
    {
        header("Content-Type:text/html; charset=utf-8");
        define('ADMIN_ID', isAdminLogin());
        if (! ADMIN_ID) {
            redirect(U('admin/login/login'));
        }
        
        define('IS_ROOT', isAdministrator());
        $allow_ip = getConfig('admin_allow_ip');
        if (! IS_ROOT && $allow_ip) {
            if (! in_array(get_client_ip(), explode(',', $allow_ip))) {
                redirect('/data/admin/error.html');
            }
        }
        $access = $this->accessControl();
        if ($access === false) {
            redirect('/data/error.html');
        } elseif ($access === null) {
            if (! $this->checkRule()) {
                redirect('/data/admin/error.html');
            }
        }
    }

    /**
     * 权限检测
     */
    final protected function checkRule()
    {
        if (IS_ROOT) {
            return true;
        }
        $moduleID = getModuleID(CONTROLLER_NAME, ACTION_NAME);
        $rules = D('Admin')->getAdminRule(ADMIN_ID);
        if (empty($moduleID) || empty($rules)) {
            return false;
        }
        if (! in_array($moduleID, $rules)) {
            return false;
        }
        return true;
    }

    /**
     * action访问控制,在 **登陆成功** 后执行的第一项权限检测任务
     *
     * @return boolean|null 返回值必须使用 `===` 进行判断
     *        
     *         返回 **false**, 不允许任何人访问(超管除外)
     *         返回 **true**, 允许任何管理员访问,无需执行节点权限检测
     *         返回 **null**, 需要继续执行节点权限检测决定是否允许访问
     * @author winky <4772499@qq.com>
     */
    final protected function accessControl()
    {
        if (IS_ROOT) {
            return true;
        }
        $allow = getConfig('allow_visit');
        $deny = getConfig('deny_visit');
        $check = strtolower(CONTROLLER_NAME . '/' . ACTION_NAME);
        if (! empty($deny) && in_array_case($check, $deny)) {
            return false;
        }
        if (! empty($allow) && in_array_case($check, $allow)) {
            return true;
        }
        return null;
    }
    
    
    /**
     * @分页
     * @author winky
     *
     * @param $model 数据对象
     * @param $field 字段
     * @param $where 条件
     * @param $order 排序
     * @param $limit 条数
     * @return array $list
     */
    protected function lists($model, $field = true, $where = array(), $order = '', $limit = 10)
    {
        $options = array();
        if (is_string($model)) {
            $model = M($model);
        }
        $OPT = new \ReflectionProperty($model, 'options');
        $OPT->setAccessible(true);
        if (! empty($order)) {
            $options['order'] = $order;
        }
        if (! empty($where)) {
            $options['where'] = $where;
        }
        $options = array_merge((array) $OPT->getValue($model), $options);
        $total = $model->where($options['where'])->count();
        $page = new \Think\Page($total, $limit);
        $p = $page->show();
        $this->assign('page', $p);
        $options['limit'] = $page->firstRow . ',' . $page->listRows;
        $model->setProperty('options', $options);
        $list = $model->field($field)->select();
        return $list;
    }

    public function _empty()
    {
        redirect('/data/admin/404.html');
    }
    
    
}